﻿
#include "FirstViewController.h"
#include "DesignViewController.h"
#include "ImgSaveFinishViewControler.h"
//#include "GaussBlurFilter.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32  || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	#include "GaussBlurFilter.h"
#endif


extern std::string changeImagePath;
IFilter* g_pFilters;

std::vector<CAImage*> UserSelectImg;

bool isflag = true;

CALabel* fontlable;

//要修改图片的比例值
float scale_value;

//加载高斯模糊库
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32  || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	#pragma comment(lib, "../filter_lib/prebuilt/win32/Gauss_Filters.lib")
#endif


FirstViewController::FirstViewController()
{

}

FirstViewController::~FirstViewController()
{

}

void FirstViewController::viewDidLoad()
{
	//获取边界值
	DRect winRect = this->getView()->getBounds();
	DSize winSize = this->getView()->getBounds().size;

	CAImage* image = CAImage::create("source_pro/startpage/background.jpg");
	CAImageView* imageView = CAImageView::createWithImage(image);
	imageView->setImageViewScaleType(CAImageViewScaleTypeFitImageCrop);
	//设置显示范围
	imageView->setFrame(winRect);
	//添加到绘制
	this->getView()->addSubview(imageView);

	//添加照相机按钮(自定义按钮)  
	CAButton* camera_button = CAButton::create(CAButtonTypeCustom);
	//设置Frame
	camera_button->setFrame(DRect((winSize.width)*0.26, (winSize.height)*0.72, winSize.width*0.5, winSize.height*0.08));

	//设置button背景图(支持9宫图)
	camera_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/startpage/camera.png")));

	camera_button->setBackGroundViewForState(CAControlStateHighlighted, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/startpage/cameraselected.png")));
	//添加到绘制
	this->getView()->addSubview(camera_button);
	//添加photoshop按钮  CADipRect
	CAButton* photoShop_button = CAButton::create(CAButtonTypeCustom);
	photoShop_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/startpage/photos.png")));
	photoShop_button->setBackGroundViewForState(CAControlStateHighlighted, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/startpage/photosselected.png")));

	photoShop_button->setFrame(DRect((winSize.width)*0.26, (winSize.height)*0.82, winSize.width *0.5, winSize.height*0.08));
	this->getView()->addSubview(photoShop_button);

	//添加等待加载控件视图
	m_ploadingView = CAActivityIndicatorView::createWithFrame(DRect(0, 0, winSize.width, winSize.height));
	m_ploadingView->setStyle(CAActivityIndicatorViewStyleGrayLarge);
	this->getView()->addSubview(m_ploadingView);
	//m_ploadingView->setVisible(false);
	m_ploadingView->stopAnimating();

	//设置监听
	camera_button->addTarget(this, CAControl_selector(FirstViewController::cameraCallBackDown), CAControlEventTouchDown);
	photoShop_button->addTarget(this, CAControl_selector(FirstViewController::photoCallBackDown), CAControlEventTouchDown);

	fontlable = CALabel::createWithFrame(DRect(winSize.width*0.38, winSize.height-100, 250, 50));
	fontlable->setText(UTF8("再按一次退出程序"));
	fontlable->setFontSize(_px(24));
	fontlable->setColor(CAColor_black); 
	this->getView()->addSubview(fontlable);
	fontlable->setVisible(false);
}

void FirstViewController::viewDidUnload()
{

}

//照相按钮按下操作
void FirstViewController::cameraCallBackDown(CAControl* control, CCPoint point)
{
	//打开照相机
	CCLog("open camera");
	CADevice::openCamera(this);
}

//图片按钮按下操作
void FirstViewController::photoCallBackDown(CAControl* control, CCPoint point)
{
	//打开手机相册
	CCLog("Open Alum");
	CADevice::openAlbum(this);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32  || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

	CAImage* image = CAImage::create("source_pro/test01.png");
	//计算该图片比例值
	scale_value = (float)image->getPixelsWide() / (float)image->getPixelsHigh();
	UserSelectImg.clear();
	 
	if (image->getPixelsWide() > 1080 && image->getPixelsHigh() >  1920)
	{
		CAImage* real_image = CAImage::scaleToNewImageWithImage(image, DSize(scale_value * 1200, 1200));
		UserSelectImg.push_back(real_image);
	}
	else
	{
		UserSelectImg.push_back(image);
	}
	//背景高斯模糊处理

	//保存用户图片用于修改
	scale_image = CAImage::scaleToNewImageWithImage(image, DSize(32, 48));   //64  96

	gauss_image = GaussBlur(scale_image);
	//保存高斯模糊之后的CAimage
	UserSelectImg.push_back(gauss_image);

	//跳转视图 将_DesignViewController压入栈顶并弹出
	RootWindow::getInstance()->getRootNavigationController()->setNavigationBarHidden(true, false);
	DesignViewController* _DesignViewController = new DesignViewController();
	_DesignViewController->init();

	RootWindow::getInstance()->getRootNavigationController()->pushViewController(_DesignViewController, true);
	_DesignViewController->release();

#endif
}


void FirstViewController::getSelectedImage(CAImage *image)
{
	//计算该图片比例值
	scale_value = (float)image->getPixelsWide() / (float)image->getPixelsHigh();
	UserSelectImg.clear();

	if (image->getPixelsWide() > 1080 && image->getPixelsHigh() > 1920)
	{
		CAImage* real_image = CAImage::scaleToNewImageWithImage(image, DSize(scale_value * 1200, 1200));
		UserSelectImg.push_back(real_image);
	}

	else
	{
		UserSelectImg.push_back(image);
	}
	//背景高斯模糊处理
	//保存用户图片用于修改
	scale_image = CAImage::scaleToNewImageWithImage(image, DSize(32, 48));   //64  96
	 
	gauss_image = GaussBlur(scale_image);
	//保存高斯模糊之后的CAimage
	UserSelectImg.push_back(gauss_image);

	//跳转视图 将_DesignViewController压入栈顶并弹出
	RootWindow::getInstance()->getRootNavigationController()->setNavigationBarHidden(true, false);
	DesignViewController* _DesignViewController = new DesignViewController();
	_DesignViewController->init();

	RootWindow::getInstance()->getRootNavigationController()->pushViewController(_DesignViewController, true);
	_DesignViewController->release();
}

//背景高斯模糊效果
CAImage* FirstViewController::GaussBlur(CAImage* image)
{ 
	//设置高斯模糊的半径
	float sigma0 = 15.5f;

	void* pInfo = &sigma0;
	g_pFilters = new CGaussBlurFilter();
	g_pFilters->Init(pInfo);

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();
	unsigned int pixelsWide = image->getPixelsWide();
	unsigned int pixelsHigh = image->getPixelsHigh();
	unsigned long dataLenght = image->getDataLenght();

	//图片数据的大小
	unsigned char* data = static_cast<unsigned char*>(malloc(dataLenght * sizeof(unsigned char)));
	memcpy(data, image->getData(), dataLenght);
	g_pFilters->Filter(data, data, pixelsWide, pixelsHigh, 32);
	g_pFilters->Reset();
	return CAImage::createWithRawDataNoCache(data, pixelFormat, pixelsWide, pixelsHigh);
}